<?php


namespace App\Engine;


use App\Exceptions\ApiException;


use App\Models\SysConfig;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;

class TokenService
{
    protected $tokenExpire;

    function __construct()
    {
        $this->tokenExpire = config('setting.token_expire_in'); //token缓存有效时间
    }

    //生成随机token
    public static function generateToken()
    {
        //32个字符组成一组随机字符串
        $randChars = self::getRandChar(32);
        //用三组字符串，进行md5加密
        $timestamp = $_SERVER['REQUEST_TIME_FLOAT'];
        //salt 盐
        $salt = config('setting.user_salt');
        return md5($randChars . $timestamp . $salt);
    }

    //通过token获取该条缓存数据中指定的字段
    public static function getCurrentTokenVar($key)
    {
        $token=BaseController::$requests->header('token');

        if (!$token) {
            throw new ApiException("token不存在",401);
        }

        $vars = Cache::get($token);

        if (!$vars) {

            throw new ApiException("键值不存在",401);
        } else {
            if (!is_array($vars)) {
                $vars = json_decode($vars, true);
            }


            if (array_key_exists($key, $vars)) {
                return $vars[$key];
            } else {
                throw new ApiException('尝试获取的变量并不存在',401);
            }
        }
    }

    //放入缓存
    public function saveCache($cachedValue)
    {
        $key = self::generateToken();//生成token
        $value = json_encode($cachedValue);

       $request=Cache::put($key,$value,7200);
        if (!$request) {
            throw new ApiException('服务器缓存异常',401);
        }
        return $key;
    }


    //通过token获取uid
    public static function getCurrentUid()
    {
        $uid = self::getCurrentTokenVar('uid');
        return $uid;
    }

    //通过token获取uid
    public static function getCurrentActive()
    {
        $uid = self::getCurrentTokenVar('active');
        return $uid;
    }

    //通过token获取uid
    public static function getCurrentAid()
    {

        $uid = self::getCurrentTokenVar('aid');
        return $uid;
    }

    public static function getCurrenAdminName()
    {
        return self::getCurrentTokenVar('admin_name');
    }

    public static function getCurrentJob()
    {
        $job = self::getCurrentTokenVar('job');
        return $job;
    }

    public static function getManageRule()
    {
        $rule = self::getCurrentTokenVar('rule');
        return $rule;
    }

    //通过token获取uid
    public static function getCurrentSid()
    {

        $uid = self::getCurrentTokenVar('sid');
        return $uid;
    }

    public static function getCurrentSessionKey()
    {
        $key=self::getCurrentTokenVar('session_key');
        return $key;

    }



    //生成token函数中调用的，生成随机字符串
    private static function getRandChar($length)
    {
        $str = null;
        $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
        $max = strlen($strPol) - 1;
        for ($i = 0;
             $i < $length;
             $i++) {
            $str .= $strPol[rand(0, $max)];
        }
        return $str;
    }

    //验证toen
    public static function verifyToken()
    {
        $token=BaseController::$requests->header('token');
        $exist = Cache::get($token);
        if ($exist) {
            return true;
        } else {
            return false;
        }
    }

    public static function CheckTokenExpire($token)
    {
        $exist = Cache::get($token);
        if ($exist) {
            return true;
        } else {
            return false;
        }
    }

}
